package com.gss.module.generator.core;

import cn.hutool.core.collection.CollUtil;
import com.gss.module.generator.constants.CommonConstants;
import com.gss.module.generator.entity.BaseEntity;
import com.gss.module.generator.entity.PomEntity;
import com.gss.module.generator.entity.ProjectEntity;

import java.io.File;
import java.util.List;

/**
 * @author DYang
 * @date 2023/5/30
 * 生成项目
 */
public class ProjectGenerator {

    /**
     * 构建项目
     * @param projectEntity
     */
    public static void builder(ProjectEntity projectEntity){
        String projectDir = projectEntity.getProjectDir();
        String artifactId = projectEntity.getArtifactId();
        artifactId = artifactId.replace(".","/");
        //创建目录
        createProject(projectDir,artifactId);
        //创建pom.xml文件
        createPomXml(projectEntity);
        //创建实体类
        createEntity(projectEntity);
    }

    /**
     * 创建实体类
     * @param projectEntity
     */
    private static void createEntity(ProjectEntity projectEntity) {
        List<BaseEntity> entityList = projectEntity.getBaseEntityList();
        if (CollUtil.isEmpty(entityList)) {
            return;
        }
        String artifactId = projectEntity.getArtifactId();
        artifactId = artifactId.replace(".","/");
        String fileDir = projectEntity + "/" + CommonConstants.PROJECT_RESOURCE + "/" + artifactId;
        for (BaseEntity baseEntity : entityList) {
            EntityGenerator.builder(fileDir,baseEntity);
        }
    }

    /**
     * 创建pom.xml
     * @param projectEntity
     */
    private static void createPomXml(ProjectEntity projectEntity) {
        PomEntity pomEntity = new PomEntity();
        pomEntity.artifactId(pomEntity.getArtifactId())
                .groupId(pomEntity.getGroupId());
        PomXmlGenerator.builder(pomEntity,projectEntity.getProjectDir());
    }

    /**
     * 构建项目
     * @param projectDir
     * @param artifactId
     */
    private static void createProject(String projectDir, String artifactId) {
        //创建源码目录
        String path = projectDir + "/" + CommonConstants.PROJECT_JAVA + "/" + artifactId;
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        path = projectDir + "/" + CommonConstants.PROJECT_RESOURCE;
        file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
    }
}
